local skel = fk.CreateSkill{
  name = "mahjong_actv_skill&"
}

local KleeU = require "packages/gm_mahjong/utility/k_utility"
local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["mahjong_actv_skill&"] = "执行",
  ["interaction_zimo"] = "自摸",
  ["interaction_hide_kong"] = "暗杠",
  ["interaction_richi"] = "立直",
  ["@@mahjong_richi"] = "立直"
}

skel:addEffect("active",  {
  mute = true,
  interaction = function (self, player)
    local choices = KleeU.checkAction(player)
    table.insert(choices, "debug")
    return UI.ComboBox{
      choices = choices
    }
  end,
  card_filter = function (self, player, to_select, selected, selected_targets)
    return self.interaction.data == "interaction_richi" and #selected == 0 and #KleeU.checkTing(player, to_select) > 0
  end,
  target_filter = Util.FalseFunc,
  feasible = Util.TrueFunc,
  can_use = function (self, player)
    return true or (#KleeU.checkAction(player) > 0 and player:getMark("cpk-turn") == 0)
  end,
  on_use = function (self, room, effect)
    local player = effect.from
    if self.interaction.data == "interaction_kong" then
      local cardtable = {}
      local cards = KleeU.getHandCards(player, 0)
      for _, cid in ipairs(cards) do
        local n = KleeU.Mahjong:new(cid):getCardName()
        cardtable[n] = (cardtable[n] or {})
        table.insert(cardtable[n], cid)
      end
      local kongtable = {}
      for _, cds in pairs(cardtable) do
        if #cds >= 4 then
          table.insert(kongtable, table.simpleClone(cds))
        end
      end
      if #kongtable == 1 then
        for _, cid in ipairs(kongtable[1]) do
          room:addCardMark(Fk:getCardById(cid), "@@Kong", 1)
        end
      else
        local listNames = {}
        for index, _ in ipairs(kongtable) do
          table.insert(listNames, "杠"..index)
        end
        local cardss = U.askForChooseCardList(room, player, listNames, kongtable, 1, 1, skel.name, "", false, false)
        for _, cid in ipairs(cardss[1]) do
          room:addCardMark(Fk:getCardById(cid), "@@Kong", 1)
        end
      end
      room:endTurn()
      player:gainAnExtraTurn(true, skel.name)
    elseif self.interaction.data == "interaction_zimo" then
      KleeU.win({player}, true)
    elseif self.interaction.data == "interaction_richi" then
      local c = Fk:getCardById(effect.cards[1])
      room:setCardMark(c, "@@mahjong_richi", 1)
      room:setPlayerMark(player, "@@mahjong_richi", 1)
      room:useCard{
        card = c,
        from = player,
        tos = {player}
      }
    elseif self.interaction.data == "debug" then
      dbg()
    end
  end
})

return skel
